﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace RLZY
{
    public partial class FormGJJB : Form
    {
        public FormGJJB()
        {
            InitializeComponent();
            
        }
        //public FormBDX_Modify(string username,String 权限)
        //{
        //    InitializeComponent();
        //    this.userID= strTextBox1Text;
        //}
        private static FormGJJB childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormGJJB ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormGJJB();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }
       
        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            
        }

     

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        
       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

        

        /// <summary>
        /// Toggles the buttons used for exporting the grid data.
        /// </summary>
        /// <param name="enabled">Boolean, used to signify whether or not to enable/disable the export buttons.</param>
        



        public string LastDate { get; set; }

        private void FormNT_Show_FormClosing(object sender, FormClosingEventArgs e)
        {
            others.RYFB fb = new RYFB();
            fb.DropGZ1();
        }

        
        private void btn打印四金通知书_Click(object sender, EventArgs e)
        {
            others.SJ sj = new SJ();
            sj.DropGZ1();
            sj.calGJJ(cmbBM.Text, this.dtpDate.Text);
            DataSet ds = sj.GetGJJ();
            this.BindData(ds);
            ToExcel();
            

        }

        
        private void ToExcel()
        {
            try
            {
                //模板文件
                string TempletFileName = "XYGJJB.xls";
                //导出文件
                string ReportFileName = "C:\\XYGJJB_New.xls";
                FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);

                ISheet ws = hssfworkbook.GetSheet("Sheet1");
                //添加或修改WorkSheet里的数据


                #region
                double number = Convert.ToDouble(this.ultraGrid1.Rows[0].Cells[0].Value.ToString().Trim());
                double money = Convert.ToDouble(this.ultraGrid1.Rows[0].Cells[1].Value.ToString().Trim());

                ws.GetRow(2).GetCell(1).SetCellValue(this.cmbBM.Text.Trim());//部门
                ws.GetRow(1).GetCell(1).SetCellValue(this.dtpDate.Text);//年份
                ws.GetRow(4).GetCell(6).SetCellValue(number.ToString()); //人数

                ws.GetRow(7).GetCell(6).SetCellValue(money.ToString()); //单位总额
                ws.GetRow(8).GetCell(6).SetCellValue(money.ToString()); //个人总额
                ws.GetRow(6).GetCell(6).SetCellValue((money * 2).ToString()); //汇缴总额
                ws.GetRow(5).GetCell(6).SetCellValue((money * 20).ToString()); //工资总额


                #endregion
                ws.ForceFormulaRecalculation = true;

                using (FileStream filess = File.OpenWrite(ReportFileName))
                {
                    hssfworkbook.Write(filess);
                }
                System.Diagnostics.Process.Start("explorer.exe", @"C:\XYGJJB_New.xls");
            }
            catch (Exception ex) { LOG.Error(ex); }

            //System.Diagnostics.Process.Start(Application.StartupPath + "C:\\RLZY_Prn\\SJTZS_Tmp_New.xls");
        }

        public decimal x1 { get; set; }

        public int x11 { get; set; }

        public int x12 { get; set; }

        public decimal x2 { get; set; }

        public decimal x3 { get; set; }

        public int x13 { get; set; }

        public int x14 { get; set; }

        public decimal x4 { get; set; }

        public int x15 { get; set; }

        public decimal x6 { get; set; }

        public decimal x5 { get; set; }
       
    }
}
